﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Sparkle.UI
{
    //http://blog.andrew-veresov.com/?tag=/silverlight

    public static class ValidationExtender
    {
        public static void SetErrorState(this Control control, string message)
        {
#if SILVERLIGHT
            control.SetValue(Validation.ErrorsProperty, ValidationErrorBuilder.GetValidationErrors(message));
            control.SetValue(Validation.HasErrorProperty, true);
            VisualStateManager.GoToState(control, "InvalidUnfocused", true);
#else

#endif

            control.GotFocus +=  ControlGotFocus;
            control.LostFocus += ControlLostFocus;
        }

        static void ControlLostFocus(object sender, RoutedEventArgs e)
        {
#if SILVERLIGHT
            var control = sender as Control;
            if (control != null && (Boolean)control.GetValue(Validation.HasErrorProperty))
            {
                VisualStateManager.GoToState(control, "InvalidUnfocused", true);
            }
#else

#endif
        }

        static void ControlGotFocus(object sender, RoutedEventArgs e)
        {
#if SILVERLIGHT
            var control = sender as Control;
            if (control != null && (Boolean)control.GetValue(Validation.HasErrorProperty))
            {
                VisualStateManager.GoToState(control, "InvalidFocused", true);
            }
#else

#endif

        }

        public static void ClearErrorState(this Control control)
        {
#if SILVERLIGHT
            control.GotFocus -= ControlGotFocus;
            control.LostFocus -= ControlLostFocus;
            control.SetValue(Validation.ErrorsProperty, null);
            control.SetValue(Validation.HasErrorProperty, false);
            VisualStateManager.GoToState(control, "Valid", true);
#else

#endif


        }
    }

}
